package com.bestcem.xm.member.controller;

import cn.hutool.core.util.StrUtil;
import com.bestcem.xm.component.security.annotation.AuthIgnore;
import com.bestcem.xm.member.dao.MemberDao;
import com.bestcem.xm.member.dao.SyncRuleDao;
import com.bestcem.xm.member.entity.pojo.MemberDO;
import com.bestcem.xm.member.entity.pojo.SyncRuleDO;
import com.bestcem.xm.member.enums.MemberStatusEnum;
import com.bestcem.xm.member.enums.MemberTypeEnum;
import com.bestcem.xm.member.mq.message.SyncWechatAccountInfoMessage;
import com.bestcem.xm.member.mq.message.WechatOfficialAccountGrantedMessage;
import com.bestcem.xm.member.service.WechatService;
import com.bestcem.xm.member.service.mq.send.MemberMessageSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 微信同步测试
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2022/4/8
 */
@Slf4j
@RestController
@RequestMapping("/member")
public class WechatSyncController {

//    @Autowired
//    private RabbitMessageSender rabbitMessageSender;
//
//    @Autowired
//    private RabbitMqConstantConfig rabbitMqConstantConfig;

    @Autowired
    private WechatService wechatService;

    @Resource
    private MemberDao memberDao;

    @Resource
    private SyncRuleDao syncRuleDao;

    @Autowired
    private MemberMessageSendService memberMessageSendService;

    @AuthIgnore
    @GetMapping(value = "/wxSync")
    public String wxSync(String orgId) {
        if (StrUtil.isNotBlank(orgId)) {
            // 判断该组织是否开启微信同步
            SyncRuleDO syncRuleDO = syncRuleDao.getByOrderIdAndMemberType(orgId, MemberTypeEnum.INDIVIDUAL.getIndex());
            if (syncRuleDO != null && syncRuleDO.getWechatSyncEnabled() == 0) {
                wechatService.sendWxSyncMessage(orgId);
            } else {
                return StrUtil.format("该组织 {} 没有开启微信同步", orgId);
            }
        }
        return "success";
    }

    @AuthIgnore
    @PostMapping(value = "/syncWechatAccountInfo")
    public MemberDO syncWechatAccountInfo(@RequestBody SyncWechatAccountInfoMessage message) {
        wechatService.syncWechatAccountInfo(message);
        // 获取该联系人
        MemberDO memberDO = memberDao.findOneByOrgIdAndMemberTypeAndStatusAndAppIdAndOpenId(message.getOrgId(), MemberTypeEnum.INDIVIDUAL,
                MemberStatusEnum.ACTIVATED, message.getAppId(), message.getOpenId());
        return memberDO;
    }

    @AuthIgnore
    @PostMapping(value = "/syncWechatOfficialAccount")
    public String syncWechatOfficialAccount(@RequestBody WechatOfficialAccountGrantedMessage message) {
        wechatService.syncWechatOfficialAccountGranted(message);
        return "success";
    }

    @AuthIgnore
    @GetMapping(value = "/sendSyncMember")
    public String sendSyncMember() {
        wechatService.publishSyncMemberSlices();
        return "success";
    }

    /**
     * 压测
     * 查看相关mq的处理速度
     *
     * @return
     */
    @AuthIgnore
    @GetMapping(value = "/yc/syncWechatAccountInfo")
    public String ycSyncWechatAccountInfo(Integer count, Integer batch) {
        for (Integer i = 0; i < count; i++) {
            SyncWechatAccountInfoMessage message = new SyncWechatAccountInfoMessage();
            message.setOrgId("624efe1faace70000f4875e0");
            message.setAppId("appId" + batch + count);
            message.setOpenId("openId" + batch + count);
            message.setUnionId("unionId" + batch + count);
//            try {
//                rabbitMessageSender.sendMsg(rabbitMqConstantConfig.getAppMemberSyncWechatAccountInfoExchange(),
//                        rabbitMqConstantConfig.getAppMemberSyncWechatAccountInfoRoutingKey(),
//                        "sdsd", message);
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//            }

            //stream mq /2022.05.12
            memberMessageSendService.syncWechatAccountInfoSend(message);

        }
        return "success";
    }

}
